package com.csyf.manager.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.csyf.manager.dao.SysTradeDao;
import com.csyf.manager.dao.TradeDto;
import com.csyf.manager.entity.SysTrade;
import com.csyf.manager.service.OrderService;
import com.csyf.manager.utils.TradeStatusEnum;
import com.csyf.manager.utils.page.JqGridVo;
import com.csyf.manager.utils.page.PageUtils;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.math.BigDecimal;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {


    @Autowired
    private SysTradeDao sysTradeDao;

    @Override
    public JqGridVo queryOrderByUserId(SysTrade vo, Integer pageIndex, Integer pageSize) {

        PageHelper.startPage(pageIndex, pageSize);
        PageHelper.orderBy("create_time desc");


        Example example = new Example(SysTrade.class);
        Example.Criteria criteria = example.createCriteria();
        PageUtils.andEqualTo(criteria, "agentId", vo.getAgentId());
        PageUtils.andEqualTo(criteria, "mchId", vo.getMchId());
        PageUtils.andEqualTo(criteria, "srcMchId", vo.getSrcMchId());
        PageUtils.andEqualTo(criteria, "srcOrderNo", vo.getSrcOrderNo());
        PageUtils.andEqualTo(criteria, "outTradeNo", vo.getOutTradeNo());
        PageUtils.andEqualTo(criteria, "serviceType", vo.getServiceType());
        PageUtils.andEqualTo(criteria, "totalFee", vo.getTotalFee());
//        PageUtils.andBetweenDate(criteria, "createTime", vo.getCreateTimeString());
//        PageUtils.andBetweenDate(criteria, "endTime", vo.getEndTime());
        PageUtils.andBetweenDateToDate(criteria, "createTime", vo.getCreateTimeString(), vo.getEndTimeString());
        PageUtils.andEqualTo(criteria, "tradeStatus", vo.getTradeStatus());
        List<SysTrade> select = sysTradeDao.selectByExample(example);

        //自定义数据列
        JqGridVo jqGridVo = PageUtils.pasrJqGrid(select);

        TradeDto tradeDto = sysTradeDao.queryTotalFeeByCondition(vo);
        JSONObject jsonObj = new JSONObject();
        jsonObj.put("outTradeNo", "总计：");
        jsonObj.put("totalFee", tradeDto.getTotalFee());
        jsonObj.put("profit", tradeDto.getProfit());
        jqGridVo.setUserdata(jsonObj);
        return jqGridVo;
    }


    @Override
    public SysTrade queryOrderByOrderNo(String orderNo) {
        SysTrade sysTrade = new SysTrade();
        sysTrade.setOutTradeNo(orderNo);
        SysTrade selectOne = sysTradeDao.selectOne(sysTrade);
        return selectOne;
    }

    @Override
    public SysTrade queryOrderByOrderNo(String mch_id, String orderNo) {
        SysTrade sysTrade = new SysTrade();
        sysTrade.setOutTradeNo(orderNo);
        sysTrade.setMchId(mch_id);
        SysTrade selectOne = sysTradeDao.selectOne(sysTrade);
        return selectOne;

    }

    @Override
    public List<SysTrade> queryWaitOrder(int pageIndex, int pagesize) {
        PageHelper.orderBy("create_time asc");
        PageHelper.startPage(pageIndex,pagesize);
        SysTrade sysTrade = new SysTrade();
        sysTrade.setTradeStatus(TradeStatusEnum.WAIT_BUYER_PAY.toString());
        return sysTradeDao.select(sysTrade);
    }

    @Override
    public BigDecimal queryJDTotalProfit(String mch_id, String date) {
       return sysTradeDao.sumJDtotalProfit(mch_id, date);
    }
}
